#!/bin/bash

#check if $1 contain $2
function Str1ContainStr2
{
	str1=$1
	str2=$2
	case  $str1 in  
		*$str2*) 
			return 1;;
		*)
			return 0;;
	esac
}

#check if file contain specific string
function FileContainStr
{

	FILE=$1
	str=$2
	if [ ! -f $FILE ];
	then 
		return 0
	fi
	tmp=$(grep -InR $str $FILE)
	if [ -z "$tmp" ];
	then
		return 0
	fi
	return 1
}

MYSYMBOL1=Johnny_bash_aliases
function IsMyFile
{

	FILE=$1
	if [ ! -f $FILE ];
	then 
		return 0
	fi
	FileContainStr $FILE $MYSYMBOL1 
	if [ $? -eq 0 ];
	then
		return 0
	fi
	return 1
}

#update the path eg: /home/ubuntu-->\/home\/ubuntu
function UpdatePathString
{
	if [ $# != 1 ];
	then
		echo "UpdatePathString Error: please input 2 strings"
		return ''
	fi
	return   echo $1 | sed 's#\/#\\\/#g'
}

#set the $1 as a global path 
function setPathGlobal
{

	PATHFILE_SYSTEM=~/.bash_aliases
	PATHFILE=$PATHFILE_SYSTEM
	PATHFILE_TEMPLATE=../bash_aliases
	TMPFILE=~/tmpXXX1             #temp file to updated original file, will be deleted afterwards
	TMPFILE2=~/tmpXXX2             #temp file to updated original file, will be deleted afterwards
	FLAG="#MyFlagForNextPath" #symbol in the file for updating the path
        BINSTALL=0

	if [ -f $PATHFILE_TEMPLATE ]; #installation mode
	then # first time 
		BINSTALL=1
		cp $PATHFILE_TEMPLATE $PATHFILE
		echo "Installation mode: replace the $PATHFILE with template"
	else 
		#is the existing path file my file? 
		IsMyFile $PATHFILE
		if [ $? -eq 0 ];
		then   
			echo "Error: the $PATHFILE is not mine file, please run install.sh to update mine first"
			return
		fi
	fi

	#check if PATHFILE already has this path string
	FileContainStr $PATHFILE "$1 "
	if [ $? -eq 1 ];
	then
		echo "Warning: Path already set! $1"
		exit 0
	fi

	#update the path string #eg /home -> \/home
	NPATH=$(echo ${1} | sed "s#\/#\\\/#g") 
	sed s/\ ${FLAG}/:${NPATH}\ ${FLAG}/g $PATHFILE>$TMPFILE
	#if not first time, then need to ask confirmation of each update
	if [ $BINSTALL -eq 0 ]	;
	then
		echo "------------Continue???---------------"
		echo "old PATH in $PATHFILE"
		cat $PATHFILE| grep PATH #show old path string
		echo "new PATH in $PATHFILE"
		cat $TMPFILE| grep PATH #show the updated file
		echo "--------------------------------------"
		#relace the old file
		conti=0
		read -p "Do you want to update the PATH string like above? y/n :" answer 
		case $answer in
			y)
				conti=1
				;;	      
			*)
				;;
		esac
		if [ "$conti" -eq "0" ];
		then
			rm $TMPFILE -rf
			return 0;
		fi
	else #installation mode
		EXPORTFLAG="#MyFlagForGoogleShellPath" #symbol for export  
		sed s/\ ${EXPORTFLAG}/${NPATH}/g $TMPFILE>$TMPFILE2
		echo "export my commands folder: $1"
	fi

	echo "replace $PATHFILE"
	if [ -f $TMPFILE2 ];
	then
		mv $TMPFILE2 $PATHFILE -f
		rm $TMPFILE2 -rf
	else
		mv $TMPFILE $PATHFILE -f
		rm $TMPFILE -rf
	fi
	source $PATHFILE
}


#set parameter 1 or current path
if [ $# == 0 ] || [ "$1" == "." ];
then
	Path=$(echo $(pwd))
else
	Path=$1
fi


echo "$(basename $0) $Path"
setPathGlobal  $Path
